home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.qualcomm.com
/
2014.06.ftp.qualcomm.com.tar
/
ftp.qualcomm.com
/
eudora
/
eudorapro
/
mac
/
extras
/
scripting
/
c-samples.hqx
/
AE to Eudora.c
next >
Wrap
Text File
|
1994-11-30
|
14KB
|
397 lines
/*************************************************************************************
* AppleEvents to Eudora *
* by Scott Gruby (sgruby@qualcomm.com) *
* *
* ⌐1994 QUALCOMM, Inc. *
* *
* Portions by Dave Perman, Shana Corp. *
*************************************************************************************/
#include <Aliases.h>
#include "Eudora AE.h"
#include <AERegistry.h>
#include <AEObjects.h>
#include <AppleEvents.h>
#include <Processes.h>
#define attachDouble 0
#define attachSingle 1
#define attachBinHex 2
#define attachUUencode 3
#define EU_Norm_Priority 0
#define EU_High_Priority 60
#define EU_Highest_Priority 1
#define EU_Low_Priority 160
#define EU_Lowest_Priority 200
void MakeNullDesc (AEDesc *theDesc);
OSErr MakeStringDesc (Str255 theStr,AEDesc *theDesc);
OSErr Eud_CreateObjSpecifier (AEKeyword theClass,AEDesc theContainer,AEKeyword theForm,AEDesc theData,
Boolean disposeInputs,AEDesc *theSpec);
OSErr MakeCurrentMsgSpec (AEDesc *theSpec);
OSErr Eud_SendEvent (AppleEvent *theEvent);
OSErr Eud_MakeEvent (AEEventClass eventClass,AEEventID eventID,AppleEvent *theEvent);
OSErr SendSetData(Str31 theFieldName,Ptr theData,long size);
OSErr Eud_SendMessage(void);
OSErr Set_Eudora_Priority(long thePriority);
/*************************************************************************************
* MakeNullDesc (AEDesc *theDesc) *
* *
* This makes a Null AppleEvent descriptor. *
*************************************************************************************/
void MakeNullDesc (AEDesc *theDesc)
{
theDesc->descriptorType=typeNull;
theDesc->dataHandle=nil;
}
/*************************************************************************************
* MakeStringDesc (Str255 theStr,AEDesc *theDesc) *
* *
* This makes a string AppleEvent descriptor. *
*************************************************************************************/
OSErr MakeStringDesc (Str255 theStr,AEDesc *theDesc)
{
AECreateDesc(kAETextSuite,&theStr[1],StrLength(theStr),theDesc);
}
/*************************************************************************************
* Eud_CreateObjSpecifier (AEKeyword theClass,AEDesc theContainer, *
* AEKeyword theForm,AEDesc theData,Boolean disposeInputs, *
* AEDesc *theSpec) *
* *
* This stuffs the required parameters into the AppleEvent. *
*************************************************************************************/
OSErr Eud_CreateObjSpecifier (AEKeyword theClass,AEDesc theContainer,AEKeyword theForm,AEDesc theData,
Boolean disposeInputs,AEDesc *theSpec)
{
AEDesc theRec;
OSErr theErr;
theErr=AECreateList(nil,0,true,&theRec);
if (!theErr)
theErr=AEPutKeyPtr(&theRec,keyAEKeyForm,typeEnumeration,&theForm,sizeof(theForm));
if (!theErr)
theErr=AEPutKeyPtr(&theRec,keyAEDesiredClass,cType,&theClass,sizeof(theClass));
if (!theErr)
theErr=AEPutKeyDesc(&theRec,keyAEKeyData,&theData);
if (!theErr)
theErr=AEPutKeyDesc(&theRec,keyAEContainer,&theContainer);
if (!theErr)
theErr=AECoerceDesc(&theRec,cObjectSpecifier,theSpec);
AEDisposeDesc(&theRec);
return theErr;
}
/*************************************************************************************
* MakeCurrentMsgSpec (AEDesc *theSpec) *
* *
* This creates an AEDesc for the current message. *
*************************************************************************************/
OSErr MakeCurrentMsgSpec (AEDesc *theSpec)
{
AEDesc theContainer,theIndex;
OSErr theErr;
long msgIndex=1;
MakeNullDesc (&theContainer);
theErr=AECreateDesc(cLongInteger,&msgIndex,sizeof(msgIndex),&theIndex);
if (!theErr)
theErr=Eud_CreateObjSpecifier(cEuMessage,theContainer,formAbsolutePosition,theIndex,true,theSpec);
AEDisposeDesc(&theContainer);
AEDisposeDesc(&theIndex);
return theErr;
}
/*************************************************************************************
* Eud_SendEvent (AppleEvent *theEvent) *
* *
* This sends a given AppleEvent to Eudora *
*************************************************************************************/
OSErr Eud_SendEvent (AppleEvent *theEvent)
{
AppleEvent theReply;
OSErr err,err1;
long theTime;
MakeNullDesc(&theReply);
err=AESend(theEvent,&theReply,kAENoReply + kAENeverInteract + kAECanSwitchLayer+kAEDontRecord,kAENormalPriority,-1,nil,nil);
AEDisposeDesc(&theReply);
AEDisposeDesc(theEvent);
return err;
}
/*************************************************************************************
* Eud_MakeEvent (AEEventClass eventClass,AEEventID eventID,AppleEvent *theEvent) *
* *
* This creates an AppleEvent that will be eventually sent to Eudora. *
* The event class and ID must be specified when calling this *
*************************************************************************************/
OSErr Eud_MakeEvent (AEEventClass eventClass,AEEventID eventID,AppleEvent *theEvent)
{
AEAddressDesc theTarget;
OSType theSignature;
OSErr theErr;
theSignature=kEudoraSuite;
theErr=AECreateDesc(typeApplSignature,&theSignature,sizeof(theSignature),&theTarget);
if (!theErr)
theErr=AECreateAppleEvent(eventClass,eventID,&theTarget,0,0,theEvent);
AEDisposeDesc(&theTarget);
return theErr;
}
/*************************************************************************************
* SendSetData(Str31 theFieldName,Ptr thePtr,long size) *
* *
* This sets the data in a specified field. It operates on the frontmost message *
* in Eudora. It is the equivalent of sending the following AppleScript: *
* set field "fieldname" of message 0 to "data" *
*************************************************************************************/
OSErr SendSetData(Str31 theFieldName,Ptr thePtr)
{
AEDesc theMsg,theName,theFieldSpec,theText;
AppleEvent theEvent;
OSErr theErr;
Handle theData;
long size=StrLength(thePtr);
theData=NewHandle(size);
BlockMove((Ptr)thePtr+1,*theData,size);
if (theData!=nil)
{
theErr=MakeCurrentMsgSpec(&theMsg);
if (!theErr)
theErr=MakeStringDesc(theFieldName,&theName);
if (!theErr)
theErr=Eud_CreateObjSpecifier(cEuField,theMsg,formName,theName,true,&theFieldSpec);
if (!theErr)
theErr=Eud_MakeEvent(kAECoreSuite,kAESetData,&theEvent);
if (!theErr)
theErr=AEPutParamDesc(&theEvent,keyAEResult,&theFieldSpec);
AEDisposeDesc(&theFieldSpec);
theText.descriptorType=typeChar;
theText.dataHandle=theData;
if (!theErr)
theErr=AEPutParamDesc(&theEvent,keyAEData,&theText);
if (!theErr)
theErr=Eud_SendEvent(&theEvent);
DisposHandle(theText.dataHandle);
AEDisposeDesc(&theText);
AEDisposeDesc(&theMsg);
AEDisposeDesc(&theName);
}
DisposHandle(theData);
return theErr;
}
/*************************************************************************************
* Eud_SendMessage(void) *
* *
* This is an example of how the AppleEvents in Eudora can be used. *
*************************************************************************************/
OSErr Eud_SendMessage(void)
{
AEDesc nullSpec,theName,theFolder,theMailbox,theInsertRec,theInsl,msgSpec,theEnclList;
OSType thePos,theClass;
AppleEvent theEvent;
Boolean trueValue;
OSErr theErr;
OSType myType;
long thePriority;
long count;
/* These are sample variables for the different fields. You are not limited to 255 characters;
this example is using Str255s to simplify things.
*/
Str255 toRecipient="\peudora-sales@qualcomm.com",
ccRecipient="\peudora-sales@qualcomm.com",
bccRecipient="\peudora-sales@qualcomm.com",
subject="\pTest Message",
body="\pThis is the message body.";
FSSpec theEnclosure;
/* This section creates a new message and places it at the end of the out mailbox.
It is equivalent to the following AppleScript:
make message at end of mailbox "out" of mail folder ""
*/
MakeNullDesc(&nullSpec);
theErr=MakeStringDesc("\p",&theName);
if (!theErr)
theErr=Eud_CreateObjSpecifier(cEuMailfolder,nullSpec,formName,theName,true,&theFolder);
if (!theErr)
theErr=MakeStringDesc("\pout",&theName);
if (!theErr)
theErr=Eud_CreateObjSpecifier(cEuMailbox,theFolder,formName,theName,true,&theMailbox);
if (!theErr)
theErr=AECreateList(nil,0,true,&theInsertRec);
if (!theErr)
theErr=AEPutKeyDesc(&theInsertRec,keyAEObject,&theMailbox);
thePos=kAEEnd;
if (!theErr)
theErr=AEPutKeyPtr(&theInsertRec,keyAEPosition,typeEnumeration,&thePos,sizeof(thePos));
if (!theErr)
theErr=AECoerceDesc(&theInsertRec,typeInsertionLoc,&theInsl);
if (!theErr)
theErr=Eud_MakeEvent(kAECoreSuite,kAECreateElement,&theEvent);
if (!theErr)
theErr=AEPutParamDesc(&theEvent,keyAEInsertHere,&theInsl);
AEDisposeDesc(&nullSpec);
AEDisposeDesc(&theName);
AEDisposeDesc(&theFolder);
AEDisposeDesc(&theMailbox);
AEDisposeDesc(&theInsertRec);
AEDisposeDesc(&theInsl);
theClass=cEuMessage;
if (!theErr)
theErr=AEPutParamPtr(&theEvent,keyAEObjectClass,cType,&theClass,sizeof(theClass));
if (!theErr)
theErr=Eud_SendEvent(&theEvent);
/* This section fills in various fields.
It is equivalent to the following AppleScript:
set field "to" of message 0 to "data"
*/
if (!theErr)
theErr=SendSetData("\pto",toRecipient);
if (!theErr)
theErr=SendSetData("\pcc",ccRecipient);
if (!theErr)
theErr=SendSetData("\pbcc",bccRecipient,);
if (!theErr)
theErr=SendSetData("\psubject",subject,);
if (!theErr)
theErr=SendSetData("\p",body,);
/* This sets the priority of the message. See the constants defined above for the legal
values.
*/
thePriority=EU_Low_Priority;
if (!theErr)
theErr=Set_Eudora_Priority(thePriority);
/* This attaches a file to the Eudora message provided it is a proper FSSpec. */
if (StrLength(theEnclosure.name)>0)
{
if (!theErr)
theErr=MakeCurrentMsgSpec(&msgSpec);
if (!theErr)
theErr=Eud_MakeEvent(kEudoraSuite,kEuAttach,&theEvent);
if (!theErr)
theErr=AEPutParamDesc(&theEvent,keyAEResult,&msgSpec);
if (!theErr)
theErr=AECreateList(nil,0,false,&theEnclList);
if (!theErr)
theErr=AEPutPtr(&theEnclList,0,typeFSS,&theEnclosure.name,sizeof(theEnclosure.name);
if (!theErr)
theErr=AEPutParamDesc(&theEvent,keyEuDocumentList,&theEnclList);
if (!theErr)
theErr=Eud_SendEvent(&theEvent);
AEDisposeDesc(&msgSpec);
AEDisposeDesc(&theEnclList);
}
/* This tells Eudora to queue the current message. */
if (!theErr)
theErr=MakeCurrentMsgSpec(&msgSpec);
if (!theErr)
theErr=Eud_MakeEvent(kEudoraSuite,kEuQueue,&theEvent);
if (!theErr)
theErr=AEPutParamDesc(&theEvent,keyAEResult,&msgSpec);
AEDisposeDesc(&msgSpec);
if (!theErr)
theErr=Eud_SendEvent(&theEvent);
}
/*************************************************************************************
* Set_Eudora_Priority(long thePriority) *
* *
* Given the priority of a message, this sets the priority in the message. *
* This same type of procedure can be used for many of the AppleScript commands in *
* the form of: *
* set <item> of message 0 to <data> *
*************************************************************************************/
OSErr Set_Eudora_Priority(long thePriority)
{
AEDesc theMsg,theData,thePropSpec,thePriorityDesc;
AppleEvent theEvent,theReply;
OSErr theErr;
AEKeyword theProperty;
Handle h;
theErr = MakeCurrentMsgSpec(&theMsg);
theProperty = pEuPriority;
AECreateDesc(typeType,&theProperty,sizeof (theProperty),&thePriorityDesc);
if (!theErr)
theErr=Eud_CreateObjSpecifier(typeProperty,theMsg,typeProperty,thePriorityDesc,true,&thePropSpec);
if (!theErr)
theErr=Eud_MakeEvent(kAECoreSuite,kAESetData,&theEvent);
if (!theErr)
theErr=AEPutKeyDesc(&theEvent, keyDirectObject, &thePropSpec);
if (!theErr)
theErr=AEDisposeDesc(&thePropSpec);
h = NewHandle (sizeof(thePriority));
BlockMove ((Ptr)&thePriority,*h,sizeof(thePriority));
theData.descriptorType = typeInteger;
theData.dataHandle = h;
if (!theErr)
theErr=AEPutParamDesc(&theEvent,keyAEData,&theData);
if (!theErr)
theErr=Eud_SendEvent(&theEvent);
DisposeHandle(h);
AEDisposeDesc(&theMsg);
AEDisposeDesc(&theData);
AEDisposeDesc(&thePriorityDesc);
return theErr;
}